package cn.tedu.egweibo0416.controller;

import cn.tedu.egweibo0416.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Api(tags="04.文件上传模块")
@RestController
@RequestMapping("/v1/file/")
public class UploadController {
    @PostMapping("upload")
    @ApiOperation(value="文件上传功能")
    @ApiOperationSupport(order=10)
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        log.debug("原始文件名："+filename);
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID()+suffix;
        log.debug("文件名：{}，",filename);
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String dataPath = sdf.format(new Date());
        String staticPath = "d:/files";
        File dirFile = new File(staticPath+dataPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath =staticPath + dataPath + filename;
        file.transferTo(new File(filePath));
        return JsonResult.ok(dataPath + filename);
    }
}
